package com.qf.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.attendance.entity.Merits;
import com.qf.attendance.mapper.MeritsMapper;
import com.qf.attendance.qo.MeritsQO;
import com.qf.attendance.service.MeritsService;
import com.qf.attendance.vo.MeritsApproveVO;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.utils.CommonBeanUtils;
import com.qf.common.db.utils.PageCommonBeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MeritsServiceImpl implements MeritsService {
    @Resource
    private MeritsMapper meritsMapper;

    /**
     * 分页查询绩效信息
     *
     * @param page 页码
     * @param size 条数
     * @return
     */
    @Override
    public IPage<MeritsApproveVO> list(int page, int size) {
        return PageCommonBeanUtils.copyPage(meritsMapper.selectPage(new Page<>(page, size), null), new Page<>(), MeritsApproveVO::new);
    }

    /**
     * 特定条件分页查询绩效信息
     *
     * @param page 页码
     * @param size 条数
     * @return
     */
    @Override
    public IPage<MeritsApproveVO> selectList(int page, int size, MeritsQO meritsQO) {
        Merits merits = new Merits();
        CommonBeanUtils.copyProperties(meritsQO, merits);
        QueryWrapper<Merits> wrapper = new QueryWrapper<>(merits);
        return PageCommonBeanUtils.copyPage(meritsMapper.selectPage(new Page<>(page, size), wrapper), new Page<>(), MeritsApproveVO::new);
    }

    /**
     * 添加绩效记录
     *
     * @param
     * @return
     */
    @Override
    public RespResult insert(MeritsQO meritsQO) {
        Merits merits = new Merits();
        CommonBeanUtils.copyProperties(meritsQO, merits);
        int count = meritsMapper.insert(merits);
        if (count > 0) {
            return RespResult.success(null);
        }
        throw new RuntimeException("添加数据失败");
    }

    /**
     * 修改
     *
     * @param meritsQO
     * @return
     */
    @Override
    public RespResult update(MeritsQO meritsQO) {
        Merits merits = new Merits();
        CommonBeanUtils.copyProperties(meritsQO, merits);
        int count = meritsMapper.updateById(merits);
        if (count > 0) {
            return RespResult.success(null);
        }
        throw new RuntimeException("数据更新失败");
    }

    /**
     * 假删除
     *
     * @param meritsId
     * @return
     */
    @Override
    public RespResult delete(int meritsId) {
        int count = meritsMapper.deleteById(meritsId);
        if (count > 0) {
            return RespResult.success(null);
        }
        throw new RuntimeException("删除失败");
    }
}
